package com.mybank.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mybank.mapper.AccountMapper;
import com.mybank.mapper.TransactionRecordMapper;
import com.mybank.pojo.Account;
import com.mybank.pojo.TransactionRecord;
import com.mybank.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;

@Transactional(rollbackFor = Exception.class)//事务回滚
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private TransactionRecordMapper transactionRecordMapper;

    /**
     * 通过卡号和密码登录
     *
     * @param account
     * @return
     */
    @Override
    public String login(Account account) {
        LambdaQueryWrapper<Account> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Account::getCardno,account.getCardno());//查询卡号
        if (ObjectUtils.isEmpty(accountMapper.selectOne(lqw))){//卡号不正确提示信息
            return "卡号不正确";
        }
        lqw.eq(Account::getPassword,account.getPassword());//查询密码
        if (ObjectUtils.isEmpty(accountMapper.selectOne(lqw))){//卡号不正确提示信息
            return "密码不正确";
        }
        if (accountMapper.selectOne(lqw).getStatus()!=1){
            return "账户已冻结";
        }
        return accountMapper.selectOne(lqw).getCardno();
    }

    /**
     * 转账操作
     *
     * @param formData
     * @return
     */
    @Override
    public String transfer(HashMap<String, String> formData) {
        LambdaQueryWrapper<Account> lqw = new LambdaQueryWrapper<>();//DQL
        LambdaQueryWrapper<Account> lqw2 = new LambdaQueryWrapper<>();//DQL
        String loginCardNo =  formData.get("loginCardNo");//登录的卡号
        String otherCardNo =  formData.get("otherCardNo");//收款方的卡号
        Double money = Double.valueOf(formData.get("money"));//交易金额
        if (ObjectUtils.isEmpty(otherCardNo) || ObjectUtils.isEmpty(money)){//非空验证
            return "账号和金额均为必填项";
        }
        lqw.eq(Account::getCardno,otherCardNo);//查询收款方账户
        Account otherAccount = accountMapper.selectOne(lqw);
        if (ObjectUtils.isEmpty(otherAccount)){//转入账户不存在
            return "转入账户不存在";
        }
        else if (otherAccount.getStatus()!=1){//转入账户被冻结
            return "转入账户被冻结";
        }
        lqw2.eq(Account::getCardno,loginCardNo);//查询登录的卡号
        Account loginAccount = accountMapper.selectOne(lqw2);
        if (loginAccount.getBalance()<money){//转出账户余额不足
            return "转出账户余额不足";
        }else {
            //登录的账户的交易记录
            TransactionRecord loginTransactionRecord = new TransactionRecord(){{
                setCardno(loginCardNo);
                setAmount(-money);
                setBalance(loginAccount.getBalance()-money);
                setTransactionType("转账");
                setRemark("转账给:"+otherCardNo);
            }};
            transactionRecordMapper.insert(loginTransactionRecord);
            //收款方的账户的交易记录
            TransactionRecord otherTransactionRecord = new TransactionRecord(){{
                setCardno(otherCardNo);
                setAmount(+money);
                setBalance(otherAccount.getBalance()+money);
                setTransactionType("转账");
                setRemark("收款来自:"+loginCardNo);
            }};
            transactionRecordMapper.insert(otherTransactionRecord);
            //登录的账户扣款
            loginAccount.setBalance(loginAccount.getBalance()-money);
            accountMapper.updateById(loginAccount);
            //收款的账户收款
            otherAccount.setBalance(otherAccount.getBalance()+money);
            accountMapper.updateById(otherAccount);
            return "操作已成功!请继续选择其它服务";
        }
    }
    /**
     * 按卡号查询余额
     * @param cardno 卡号
     * @return 余额
     */
    @Override
    public Double getBalance(String cardno) {
        LambdaQueryWrapper<Account> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Account::getCardno,cardno);
        return accountMapper.selectOne(lqw).getBalance();
    }

    @Override
    public Integer updatePassword(HashMap<String,String> params) {
        LambdaQueryWrapper<Account> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Account::getCardno,params.get("loginCardNo"));
        Account account = accountMapper.selectOne(lqw);
        account.setPassword(params.get("newPwd"));
        accountMapper.updateById(account);
        return accountMapper.updateById(account);
    }
}
